package cn.edu.zxj.ojspring.service;

import cn.edu.zxj.ojspring.controller.CompileController;
import cn.edu.zxj.ojspring.mapper.OJMapper;
import cn.edu.zxj.ojspring.model.compile.*;
import cn.edu.zxj.ojspring.model.info.OJInfo;
import cn.edu.zxj.ojspring.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-02-21
 * @time: 18:04:35
 */
@Service
@Slf4j
public class CompileService {
    @Autowired
    private OJMapper ojMapper;


    public CompileResponse compileAndRun(CompileRequest compileRequest) {
        // 1. 查询数据, 获取测试用例的代码
        OJInfo ojInfo = ojMapper.selectOJBy(compileRequest.getId());

        if (ojInfo == null) {
            log.warn("查询数据库无结果, 题目 id: {}", compileRequest.getId());
            return null;
        }

        // 测试用例的代码 -- 里面包含 main 方法 形式如下
        /*
          public static void main(String[] args) {
          	Solution solution = new Solution();
              // testcase1
              if (solution.addDigits(38) == 2) {
              	System.out.println("Test OK");
              } else {
                  System.out.println("Test failed");
              }
              // testcase2
              if (solution.addDigits(111) == 3) {
              	System.out.println("Test OK");
              } else {
                  System.out.println("Test failed");
              }
          }
         */
        String testCode = ojInfo.getTestCode();

        log.info("testCode: {}", testCode);


        // 2. 获取用户的代码
        // 用户的代码
        /*
          class Solution {
              public double findMedianSortedArrays(int[] nums1, int[] nums2) {

              }
          }
         */
        String requestCode = compileRequest.getCode();
        log.info("requestCode: {}", requestCode);


        // 3. 合并代码
        String finalCode = mergeCode(testCode, requestCode);

        log.info("最终的代码: finalCode: {}", finalCode);

        // 4. 构造 compile.Task 来实现编译运行逻辑
        Task task = new Task();
        Question question = new Question();
        question.setCode(finalCode);
        Answer answer = task.compileAndRun(question);
        if (answer == null) {
            return null;
        }

        // 5. 依据 answer 构造 CompileResponse
        CompileResponse compileResponse = new CompileResponse();
        compileResponse.setError(answer.getError());
        compileResponse.setStdout(answer.getStdoutMessage());
        compileResponse.setReason(answer.getErrorMessage());

        return compileResponse;
    }

    private static String mergeCode(String testCode, String requestCode) {
        StringBuilder tmp = new StringBuilder();
        int pos = requestCode.lastIndexOf('}');
        if (pos == -1) {
            return null;
        }
        tmp.append(requestCode, 0, pos);
        tmp.append(testCode);
        tmp.append("\n}");
        return tmp.toString();
    }


    // public static void main(String[] args) {
    //     String testCode = "          public static void main(String[] args) {\n" +
    //             "          \tSolution solution = new Solution();\n" +
    //             "              // testcase1\n" +
    //             "              if (solution.addDigits(38) == 2) {\n" +
    //             "              \tSystem.out.println(\"Test OK\");\n" +
    //             "              } else {\n" +
    //             "                  System.out.println(\"Test failed\");\n" +
    //             "              }\n" +
    //             "              // testcase2\n" +
    //             "              if (solution.addDigits(111) == 3) {\n" +
    //             "              \tSystem.out.println(\"Test OK\");\n" +
    //             "              } else {\n" +
    //             "                  System.out.println(\"Test failed\");\n" +
    //             "              }\n" +
    //             "          }";
    //     String code = "          class Solution {\n" +
    //             "              public double findMedianSortedArrays(int[] nums1, int[] nums2) {\n" +
    //             "         \n" +
    //             "              }\n" +
    //             "          }";
    //     System.out.println(mergeCode(testCode,code));
    //     FileUtils.writeContentToFile("./tmp/Solution.java",mergeCode(testCode,code));
    // }

}
